package com.talos.ums.controller.admin.socialuser;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.socialuser.vo.UmsSocialUserPageReqVO;
import com.talos.ums.controller.admin.socialuser.vo.UmsSocialUserRespVO;
import com.talos.ums.controller.admin.socialuser.vo.UmsSocialUserSaveReqVO;
import com.talos.ums.dal.dataobject.socialuser.UmsSocialUserDO;
import com.talos.ums.service.socialuser.UmsSocialUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 社交用户表")
@RestController
@RequestMapping("/ums/social-user")
@Validated
public class UmsSocialUserController {

    @Resource
    private UmsSocialUserService socialUserService;

    @PostMapping("/create")
    @Operation(summary = "创建社交用户表")
    @PreAuthorize("@ss.hasPermission('ums:social-user:create')")
    public CommonResult<Long> createSocialUser(@Valid @RequestBody UmsSocialUserSaveReqVO createReqVO) {
        return success(socialUserService.createSocialUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新社交用户表")
    @PreAuthorize("@ss.hasPermission('ums:social-user:update')")
    public CommonResult<Boolean> updateSocialUser(@Valid @RequestBody UmsSocialUserSaveReqVO updateReqVO) {
        socialUserService.updateSocialUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除社交用户表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:social-user:delete')")
    public CommonResult<Boolean> deleteSocialUser(@RequestParam("id") Long id) {
        socialUserService.deleteSocialUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得社交用户表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:social-user:query')")
    public CommonResult<UmsSocialUserRespVO> getSocialUser(@RequestParam("id") Long id) {
        UmsSocialUserDO socialUser = socialUserService.getSocialUser(id);
        return success(BeanUtils.toBean(socialUser, UmsSocialUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得社交用户表分页")
    @PreAuthorize("@ss.hasPermission('ums:social-user:query')")
    public CommonResult<PageResult<UmsSocialUserRespVO>> getSocialUserPage(@Valid UmsSocialUserPageReqVO pageReqVO) {
        PageResult<UmsSocialUserDO> pageResult = socialUserService.getSocialUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsSocialUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出社交用户表 Excel")
    @PreAuthorize("@ss.hasPermission('ums:social-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSocialUserExcel(@Valid UmsSocialUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsSocialUserDO> list = socialUserService.getSocialUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "社交用户表.xls", "数据", UmsSocialUserRespVO.class,
                        BeanUtils.toBean(list, UmsSocialUserRespVO.class));
    }

}
